package com.iteaj.network.test.server.line;

import com.iteaj.network.message.UnParseBodyMessage;
import com.iteaj.network.test.MessageCreator;
import com.iteaj.network.test.message.TMessageBody;
import com.iteaj.network.test.message.TMessageHead;
import com.iteaj.network.utils.ByteUtil;

import java.io.IOException;

public class LineMessage extends UnParseBodyMessage {

    public LineMessage(byte[] message) {
        super(message);
    }

    public LineMessage(MessageHead head, MessageBody body) {
        super(head, body);
    }

    @Override
    public LineMessage build() throws IOException {
        if(message != null) { // 客户端请求或者响应平台
            return MessageCreator.lineBuild(message);
        } else if(getHead() != null && getBody() != null) { // 平台请求或者响应客户端
            final int length = getHead().getHeadLength() + messageBody.getBodyLength();
            this.message = new byte[length];
            ByteUtil.addBytes(message, getHead().getHeadMessage(), 0);
            ByteUtil.addBytes(message, getBody().getBodyMessage(), getHead().getHeadLength());
        }

        return this;
    }

    @Override
    public TMessageHead getHead() {
        return (TMessageHead) super.getHead();
    }

    @Override
    public TMessageBody getBody() {
        return (TMessageBody) super.getBody();
    }
}
